package dimensional.traveler.server.persistence.mock;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import dimensional.traveler.server.persistence.MissionRepository;
import dimensional.traveler.shared.model.Mission;

public class MockMissionRepository implements MissionRepository {

	private Map<Integer, List<Mission>> missions = new HashMap<>(5);

	public MockMissionRepository() {
		MockMissionFactory missionFactory = new MockMissionFactory();

		Mission tutorial = missionFactory.createTutorialMission();
		List<Mission> tutorialList = new LinkedList<>();
		tutorialList.add(tutorial);
		missions.put(0, tutorialList);

		Mission romanMission = missionFactory.createRomanMission();
		Mission apacheMission = missionFactory.createApacheMission();
		List<Mission> levelOneList = new LinkedList<>();
		levelOneList.add(romanMission);
		levelOneList.add(apacheMission);
		missions.put(1, levelOneList);
	}

	@Override
	public Collection<Mission> getAvailableMissions(int numberOfCompletedMissions) {
		List<Mission> availableMissions = new LinkedList<>();
		for (Entry<Integer, List<Mission>> entry : missions.entrySet()) {
			if (entry.getKey() <= numberOfCompletedMissions) {
				availableMissions.addAll(entry.getValue());
			}
		}
		return availableMissions;
	}

	@Override
	public Mission getFirstMission() {
		return missions.get(0).get(0);
	}

	@Override
	public Mission getMissionById(String missionId) {
		for (Entry<Integer, List<Mission>> entry : missions.entrySet()) {
			for (Mission mission : entry.getValue()) {
				if (mission.getMissionId().equals(missionId))
					return mission;
			}
		}
		return null;
	}

}
